package sa4

import javax.servlet.http._
import scala.xml._
import javax.imageio.ImageIO
import javax.servlet.ServletConfig
import sa4.persistence._
import org.squeryl.KeyedEntity
import org.squeryl.SessionFactory
import org.squeryl.Session
import org.squeryl.adapters.H2Adapter
import scala.util.matching.Regex
import org.squeryl.adapters.H2Adapter
import java.sql.Timestamp
import java.sql.Date
import org.squeryl.PrimitiveTypeMode._
import org.squeryl.dsl.ast.LogicalBoolean
import org.squeryl.annotations.OptionType

class Skeleton extends Framework {
  override def init(config: ServletConfig) = {
    super.init(config)
    initRoutes
  }
  
  override def service(req: HttpServletRequest, resp: HttpServletResponse) {
    super.service(req, resp)
    //Session setup
    var currentContext = session.getCurrentContext
    currentContext = session.newContext(req, resp, currentContext)
    var cc = currentContext().session
    cc.setMaxInactiveInterval(60)
  }

  def initRoutes(): Unit = {
    ///////////////////////////////
    // TODO: Add your routes code here
    ///////////////////////////////
    
    initDefaultRoutes()
  }
  
  def initDefaultRoutes(): Unit = {
    routing.addRoute("/", startPage)
      /////////////////////////////
    //routing for templating
    //-------------------------->
    routing.addRoute("/about", loadXML)
    //<--------------------------
  }

  ///////////////////////////////
  // TODO: Add your functions here
  ///////////////////////////////
  
  def startPage(req: HttpServletRequest, resp: HttpServletResponse) {
    session.updateSession
    val xml = template.loadPage("src/main/scala/sa4/index.html")
    val html = form.prepareForm(xml, routing)
    resp.getWriter.write(new PrettyPrinter(72, 2).formatNodes(html))
    resp.setContentType("text/html")
  }

  /////////////////////////////
  //Methods to for templating
  //-------------------------->
  def loadXML(req: HttpServletRequest, resp: HttpServletResponse){
  val xml = template.processTemplate(about)
    val html = form.prepareForm(xml, routing)
    resp.getWriter.write(new PrettyPrinter(72, 2).formatNodes(html))
    if (!req.isRequestedSessionIdValid) {
      var dest = resp.encodeRedirectURL("/")
      resp.sendRedirect(dest)
    }
  }
  
  /////////////////////////////
  //XML that is inserted in the template
  //-------------------------->
  def about={
    <surround with="src/main/scala/sa4/index.html" at="here">
		  <div class="container">
		  	<div class="hero-unit">
		  	<h1> About</h1>
		  	<p>This is the Software Atelier 4 project of the USI.
		  	This framework was build in phases each of them with a different groups</p>
		  	<h2>Phase 1</h2>
		  	<p> Nicolas Latorre, Gregory Banfi, Virginie Blancs, Fatima Chegini</p>
		  	<h2>Phase 2</h2>
		  	<p>Nicolas Latorre, Giuseppe Mendola, Fatima Chegini, Gabriele Cerfoglio </p>
		  	<h2>Phase 3</h2>
		  	<p>Nicolas Latorre, Giuseppe Mendola, Gabriele Cerfoglio, Virginie Blancs</p>
		  	<h2>With help of:</h2>
		  	<p>Professor Nate Nystrom</p>
		  	<p>Assistants : Konstantin Rubinov, Saeed Aghaee</p>
		  </div>
		  </div>
	</surround>
  //<--------------------------
		  
  }
}